﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace TCC
{
    public partial class Index : System.Web.UI.Page
    {
        // private static string strConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\TCC\ownztec\ProjetoTCC\TCC\App_Data\TCCDB.mdf;Integrated Security=True;User Instance=True";

        protected void Page_Load(object sender, EventArgs e)
        {
            /*

                        var con = new SqlConnection(strConnectionString);
                        //var qry = "select * from tbFilmes tf where tf.generoFilme = " + gen + " order by hits desc";
                        var qry = "select * from tbFilmes order by hits desc";
                        var da = new SqlDataAdapter(qry, con);

                        DataSet ds = new DataSet();

                        da.Fill(ds);

                        gvListaDeFilmes.DataSource = ds;
                        gvListaDeFilmes.DataBind();
                        gvListaDeFilmes.Columns[0].Visible = false;
                        gvListaDeFilmes.Columns[1].Visible = false;
                        gvListaDeFilmes.Columns[2].Visible = false;
                        //string gen = Request.QueryString["gen"];

                        //if (string.IsNullOrEmpty(gen))
                        //{
                        //    Page.Visible = false;
                        //    return;
                        //}


                        //if (!Page.IsPostBack)
                        //{
                        //   CarregaFilmes(gen);
                        //}
                    }

                    void CarregaFilmes(string gen)
                    {
                        var con = new SqlConnection(strConnectionString);
                        //var qry = "select * from tbFilmes tf where tf.generoFilme = " + gen + " order by hits desc";
                        var qry = "select * from tbFilmes order by hits desc";
                        var da = new SqlDataAdapter(qry, con);

                        DataSet ds = new DataSet();

                        da.Fill(ds);

                        gvListaDeFilmes.DataSource = ds;
                        gvListaDeFilmes.DataBind();
                        gvListaDeFilmes.Columns[0].Visible = false;
                        gvListaDeFilmes.Columns[1].Visible = false;
                        gvListaDeFilmes.Columns[2].Visible = false;

                    }

                    void CarregaNome(string idFilme)
                    {

                        var con = new SqlConnection(strConnectionString);
                        var qry = "select nomeFilme from tbFilmes tf where tf.idFilme = " + idFilme;
                        var da = new SqlDataAdapter(qry, con);

                        da.SelectCommand.Connection.Open();

                        lblNomeFilme.Text = "teste"; //da.SelectCommand.ExecuteScalar().ToString();

                        da.SelectCommand.Connection.Close();

                    }

                    void CarregaDescricao(string idFilme)
                    {

                        var con = new SqlConnection(strConnectionString);
                        var qry = "select sinopse from tbFilmes tf where tf.idFilme = " + idFilme;
                        var da = new SqlDataAdapter(qry, con);

                        da.SelectCommand.Connection.Open();

                        lblDescFilme.Text = da.SelectCommand.ExecuteScalar().ToString();

                        da.SelectCommand.Connection.Close();

                    }

                    void CarregaDiretor(string idFilme)
                    {

                        var con = new SqlConnection(strConnectionString);
                        var qry = "select diretor from tbFilmes tf where tf.idFilme = " + idFilme;
                        var da = new SqlDataAdapter(qry, con);

                        da.SelectCommand.Connection.Open();

                        lblDiretor.Text = da.SelectCommand.ExecuteScalar().ToString();

                        da.SelectCommand.Connection.Close();

                    }

                    void AddHit(string idFilme)
                    {

                        var con = new SqlConnection(strConnectionString);
                        var qry = "select hits from tbFilmes tf where tf.idFilme = " + idFilme;
                        var da = new SqlDataAdapter(qry, con);
                        var ds = new DataSet();

                        da.SelectCommand.Connection.Open();

                        int aux = 0;
                        aux = Convert.ToInt32(da.SelectCommand.ExecuteScalar());
                        aux = aux + 1;

                        da.SelectCommand.Connection.Close();

                        var updt = "update tbFilmes set hits = " + aux + " where idFilme = " + idFilme;
                        var cmd = new SqlCommand(updt, con);

                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();

                        lblHits.Text = aux.ToString();

                    }

                    protected void gvListaDeFilmes_RowDataBound(object sender, GridViewRowEventArgs e)
                    {
                        if (e.Row.RowIndex != -1)
                        {

                            using (LinkButton lbFilme = (LinkButton)e.Row.Cells[3].FindControl("lbFilme"))
                            {
                                lbFilme.CommandArgument = e.Row.Cells[0].Text;//idfilme
                                lbFilme.CommandName = e.Row.Cells[2].Text;//idgenero
                                lbFilme.Text = e.Row.Cells[1].Text;

                            }
                        }
                    }

                    protected void gvListaDeFilmes_RowCommand(object sender, GridViewCommandEventArgs e)
                    {

                        Response.Redirect("generic.aspx?gen=");
                        //CarregaNome(e.CommandArgument.ToString());
                        //CarregaDiretor(e.CommandArgument.ToString());
                        //CarregaDescricao(e.CommandArgument.ToString());
                        //AddHit(e.CommandArgument.ToString());
                    }*/
        }
    }
}